package com.wg.msorderds.controller;

import com.wg.msorderds.entity.UserAddress;
import com.wg.msorderds.service.IUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order/user-address")
@CrossOrigin(origins = "http://localhost:5173")
public class UserAddressController {

    @Autowired
    private IUserAddressService userAddressService;

    /**
     * 添加用户地址
     */
    @PostMapping("/add")
    public boolean save(@RequestBody UserAddress userAddress) {
        return userAddressService.save(userAddress);
    }

    /**
     * 删除用户地址
     */
    @DeleteMapping("/delete")
    public boolean delete(Integer id) {
        return userAddressService.removeById(id);
    }

    /**
     * 更新用户地址
     */
    @PutMapping("/update")
    public boolean update(@RequestBody UserAddress userAddress) {
        return userAddressService.updateById(userAddress);
    }
    /**
     * 将指定用户的所有地址设置为非默认地址
     */
    @PutMapping("/set-all-non-default")
    public boolean setAllNonDefault(@RequestParam Integer userId) {
        return userAddressService.setAllNonDefaultByUserId(userId);
    }
    @PutMapping("/isdefault/{id}")
    public boolean updateDefaultById(@PathVariable Integer id) {
        return userAddressService.updateDefaultById(id)>0;
    }
    /**
     * 根据ID获取用户地址
     */
    @GetMapping("/get")
    public UserAddress get(Integer id) {
        return userAddressService.getById(id);
    }

    /**
     * 获取所有用户地址列表
     */
    @GetMapping("/list")
    public List<UserAddress> list() {
        return userAddressService.list();
    }

    /**
     * 根据用户ID获取地址列表
     */
    @GetMapping("/list-by-user")
    public List<UserAddress> listByUserId(@RequestParam Integer userId) {
        return userAddressService.listByUserId(userId);
    }
}

